from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep

# chromedriver版本为137版本
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

def login():
    """
    登录系统
    """
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

def view_search(status="disabled"):
    """
    搜索用户分层视图
    :param status: 搜索状态，可选值为 "disabled"（禁用）或 "enabled"（生效）
    """
    # 点击菜单进入用户页面
    browser.find_element(By.CSS_SELECTOR, ".menu_titre > a[title='用户和组']").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_user_hierarchy > a[title='分层视图']").click()

    # 选择状态（2为禁用，3为生效）
    status_option = "option:nth-child(2)" if status == "disabled" else "option:nth-child(3)"
    browser.find_element(By.CSS_SELECTOR, f"select#search_status>{status_option}").click()

    # 点击搜索按钮
    browser.find_element(By.NAME, "button_search_x").click()

    # 等待搜索结果加载完成（可以根据实际情况调整延时）
    sleep(2)

if __name__ == '__main__':
    try:
        login()
        # 测试禁用状态
        view_search(status="disabled")
        # 测试生效状态
        view_search(status="enabled")
    except Exception as e:
        print(e)
    finally:
        sleep(1)
        browser.quit()